<?php

namespace App\Admin\Controllers;

use App\Models\StoreDetail;
use Carbon\Carbon;
use Illuminate\Support\MessageBag;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\Auth;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class StoreDetailsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '门店详情';
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new StoreDetail());
        
        $grid->column('name', __('门店名称'));
        $grid->column('logo_url', __('Logo图标'));
        $grid->column('mobile', __('手机号码'));
        $grid->column('province', __('省'));
        $grid->column('city', __('市'));
        $grid->column('county', __('区'));
        $grid->column('address', __('详细地址'));
        $grid->column('business_time_start', __('营业开始时间'))->display(function ($time){
            if (!empty($time)){
                return date('Y-m-d H:i:s', $time);
            }
        });
        $grid->column('business_time_end', __('营业结束时间'))->display(function ($time){
            if (!empty($time)){
                return date('Y-m-d H:i:s', $time);

            }
        });;
        $grid->column('details', __('商品详情'));
        $grid->column('status', __('状态'));

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('name', '门店名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(StoreDetail::findOrFail($id));

//        $show->field('id', __('Id'));
//        $show->field('store_id', __('门店id'));
        $show->field('name', __('门店名称'));
//        $show->field('logo_url', __('Logo图标'));
        $show->field('mobile', __('手机号码'));
//        $show->field('province', __('省'));
//        $show->field('city', __('市'));
//        $show->field('county', __('区'));
//        $show->field('address', __('详细地址'));
//        $show->field('lat', __('纬度'));
//        $show->field('lng', __('经度'));
        $show->field('business_time_start', __('营业开始时间'));
        $show->field('business_time_end', __('营业结束时间'));
//        $show->field('details', __('商品详情'));
//        $show->field('status', __('状态'));
        $show->field('created_at', __('创建时间'));
//        $show->field('updated_at', __('更新时间'));

        // 隐藏删除和列表按钮
        $show->panel()
            ->tools(function ($tools) {
//                $tools->disableEdit();
                $tools->disableList();
                $tools->disableDelete();
            });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new StoreDetail());

        $form->text('store.name', '门店名称')->rules("required");
        $form->hidden('name', __('门店名称'));
        $form->image('logo_url', __('Logo图标'))->required();
        $form->text('mobile', '联系电话')->rules('required');
        $form->text('sn', __('Sn'))->required();
        $form->text('merchant_id', __('Merchant Id'))->required();

        $form->distpicker(['province', 'city', 'county'], '地址选择')->required();

        $form->text('address', __('详细地址'))->required();
        $form->latlong('lat', 'lng', '位置');
        $form->time('business_time_start','营业开始时间')->format('H:mm:ss');
        $form->time('business_time_end','营业结束时间')->format('H:mm:ss');
        $form->editor('details', __('详情'))->required();

        $form->switch('status', __('状态'))->states(['on' => ['value' => 1, 'text' => '开启', 'color' => 'success'],
	        'off' => ['value' => 2, 'text' => '修整', 'color' => 'danger']]);
        // 保存前调用
        $form->saving(function (Form $form) {
        	$start = new Carbon($form->business_time_start);
        	$end = new Carbon($form->business_time_end);
        	//dd($form, $start->toObject()->timestamp, $end->toObject()->timestamp);
	        //dd(strtotime($form->business_time_start), strtotime($form->business_time_end));
            $form->business_time_start = $start->toObject()->timestamp;
            $form->business_time_end = $end->toObject()->timestamp;
        });
        $form->saved(function (Form $form) {

            // 跳转页面
            $success = new MessageBag([
                'title'   => '标题',
                'message' => '成功',
            ]);
            return back()->with(compact('success'));
        });
        $form->tools(function (Form\Tools $tools) {

            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            $tools->disableView();

            // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//            $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
        });

        $form->footer(function (Form\Footer $footer) {
            $footer->disableCreatingCheck();
            $footer->disableEditingCheck();
            $footer->disableReset();
        });

        return $form;
    }

    public function storeDetail()
    {
        $id = Auth::guard('admin')->user()->store_id;
        $ids = StoreDetail::where('store_id',$id)->get()->pluck( 'id')[0];
        return redirect()->route('storedetails.edit', ['storedetail' => $ids]);
    }
}
